package com.dycong.common.IO.netty.ssl.server;

import com.dycong.common.IO.netty.first.EchoServerHandler;
import com.dycong.common.IO.netty.ssl.client.SecureSocketSslContextFactory;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.ssl.SslHandler;

import javax.net.ssl.SSLEngine;


/**
 * Created by dycong on 2017/4/5.
 */
public class SecureServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    public  void initChannel(SocketChannel ch) throws Exception{
        SSLEngine sslEngine= SecureSocketSslContextFactory.getServerContext().createSSLEngine();
        sslEngine.setUseClientMode(false);
        sslEngine.setNeedClientAuth(true);

        ch.pipeline().addLast("SSL",new SslHandler(sslEngine))
        .addLast(new StringDecoder())
        .addLast(new StringEncoder())
        .addLast(new EchoServerHandler())
        ;
    }

}
